package com.ant.collect.service.impl;

import com.ant.collect.dto.MaterialProductDto;
import com.ant.collect.mapper.MaterialProductMapper;
import com.ant.collect.pojo.MaterialProduct;
import com.ant.collect.service.IMaterialProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-31
 */
@Service
public class MaterialProductServiceImpl extends ServiceImpl<MaterialProductMapper, MaterialProduct> implements IMaterialProductService {

    @Override
    public List<MaterialProduct> listById(Integer id) {
        return baseMapper.selectList(new QueryWrapper<MaterialProduct>().eq("parent_id",id));
    }

    @Override
    public MaterialProduct saveMaterialProduct(MaterialProductDto dto) {
        if(dto.getParentId()== null){
            throw new RuntimeException("请选择企业");
        }
        MaterialProduct year = new MaterialProduct();
        year.setParentId(dto.getParentId());
        year.setVocName(dto.getVocName());
        year.setVocType(dto.getVocType());
        year.setVocDosage(dto.getVocDosage());
        year.setVocContent(dto.getVocContent());
        year.setVocQuality(dto.getVocQuality());
        if(dto.getId()==null){
            baseMapper.insert(year);
        }else {
            year.setId(dto.getId());
            baseMapper.updateById(year);
        }
        return year;
    }
}
